define(function(require, exports, module) {
    var router = require("router");
    //var app = require("app");
    var controller = {};
    controller.vue = function() {
        controller.vm = new Vue({
            router: router,
            data: function() {
                return {
                  data:{}, 
                  userInfo: {},
                  address: '',
                  date: '',
                  book_time:'',

                  dining:{},
                  shop_id:213,
                  use_yie: 0,
                  card_code: '',
                  card_title:'',
                  people:'',
                  dining_table:'',
                  shop_name:'',
                  user_info:{},
                  shop_info:{},
                  shop_list_select:[],
                  loading: 0,
                  disabled: 0,
                  order_amount:0,
                  dining_table_list:[],
                  numbers: ['1人', '2人', '3人', '4人', '5人', '6人', '7人', '8人', '9人','10人以上'],
                };
            },
            el: '#pageIndex',
            created: function() {
                this.fetchData();
            },
              mounted: function() {
                    
                    if (this.mounted == 1) {
                        return;
                    }
                    this.bindPick();
                    var that = this ;
                this.mounted = 1;
            },

            methods: {
                fetchData: function() {
                    var _this = this;
                    this.get_shop_list();
                    this.getInfo();
                },
                bindPick:function(){
                  var that = this;
                  app.picker("#people",{
                          title: "选择人数",
                          cols: [
                       
                              {
                                  textAlign: 'center',
                                  values: that.numbers
                              }
                          ],
                          onClose: function(p) {
                               var v = p.displayValue;
                               that.bindPickerChange("people",v);
                              
                          },
                    });
                  // 时间
                  app.datetimePickerWap("#time",{
                      title: '选择时间',
                      // years: [2017, 2018],
                      // monthes: ['06', '07'],
                      onChange: function (picker, values, displayValues) {
                          // console.log(picker);
                          var date = displayValues[0]+"-"+displayValues[1]+"-"+displayValues[2];
                          var time =displayValues[3]+":"+displayValues[4];
                          var book_time = date+" "+time;
                          that.setData({ book_time: book_time,date:date,time:time });
                      }
                  });
                  
                },
                bindPickerChange: function (type,values) {
                  var v ;
                  if(typeof (values) =="string"){
                    v = values;
                  }else if(values && values.length>0){
                    v = values[0];
                  }
                   var index;
                  if (type == "people") {
                    this.setData({ people: v});
                  } else if (type== "dining_table"){
                     index = this.indexOf(this.dining_table_list_select, v);
                     if(!this.dining_table_list[index]){
                      return ;
                     }
                    this.setData({ dining: this.dining_table_list[index], dining_table: this.dining_table_list[index].name + "(" + this.dining_table_list[index].remark+")" });
                    this.loadBookCart();
                  } else if (type == "shop") {
                     index = this.indexOf(this.shop_list_select, v);
                     if(!this.shop_list[index]){
                      return ;
                     }
                    this.setData({ shop: this.shop_list[index], shop_name: v, shop_id: this.shop_list[index].id  });
                    this.get_dining_table();
                  }
                },
                indexOf:function(arr,item){
                      var index = -1;
                      for( var i=0;i<arr.length;i++){
                         if(arr[i]===item){
                           index= i;
                           break;
                         }
                      }
                      return index;
                },

              //预约类型
                get_dining_table: function () {
                  var that = this;
                  app.get("mall/dining_table/get", {
                    shop_id: this.shop_id,
                  }, function (res) {
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                    var list = res.data.list;
                    console.log("res", list);
                    var dining_table_list_select = [];
                    for (var i in list) {
                      dining_table_list_select.push(list[i].name);
                    }
                    // console.log(dining_table_list_select);
                    that.setData({ dining_table_list: list, dining_table_list_select: dining_table_list_select });
                    app.picker("#dining_table",{
                          title: "选择预约类型",
                          cols: [
                       
                              {
                                  textAlign: 'center',
                                  values: that.dining_table_list_select
                              }
                          ],
                          onClose: function(p) {
                               var v = p.displayValue;
                               that.bindPickerChange("dining_table",v);
                              // that.bindChange(p, v, dv);
                          },
                      });

                  });
                },
                //计算金额
              loadBookCart: function (reload){
                if (reload == 1) {
                  this.setData({
                    loading: 0,
                  });
                }
                var id = this.dining.id;
                var that = this;
                app.get("mall/dining_table_book/discounts", {
                  shop_id: this.shop_id,
                  card_code: this.card_code,
                  diningTableId:id,
                }, function (res) {
                  that.data.loading = 0;
                  if (res.code != 200) {
                    return;
                  }
                  if (res.data.cardRes && res.data.cardRes.code > 0) {
                    app.alert(res.data.cardRes.msg);
                    that.card_code = "";
                    that.card_title = "";
                  }
                  that.setData({
                    data: res.data,
                    order_amount:res.data.total_price
                  });
                 
                });
              },
                      
                //用户信息
            getInfo: function () {
              var that = this;
              app.get("api/user/info", {
                fields: "avatarUrl,user_id,last_update_time,nickname,phone"
              }, function (res) {

                if (res.code != 200) {
                 // app.alert(res.msg);
                  return;
                }

                that.setData({ user_info: res.data });
              });
            },
            //选择卡券
            chooseCard:function(){
                  var that = this;
                  app.component("card",{
                     success:function(res){
                        that.$router.go(-1);
                        that.card_code = res.card_code;
                        that.card_title = res.card_title;
                        that.loadBookCart(1);
                     }
                  });
                },
                        
            //分店列表
              get_shop_list: function () {
                  var that = this;
                  app.get("mall/shop/get", {
                  }, function (res) {
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return;
                    }
                    var list = res.data.list;
                    
                    var shop_list_select=[];
                    for (var i in list){
                      shop_list_select.push(list[i].name);
                    }

                    that.setData({ shop_list: list, shop_list_select: shop_list_select});
                    that.bindPickerChange("shop",shop_list_select[0]);
                    app.picker("#shop_name",{
                          title: "选择门店",
                          cols: [
                       
                              {
                                  textAlign: 'center',
                                  values: that.shop_list_select
                              }
                          ],
                          onClose: function(p) {
                               var v = p.displayValue;
                               that.bindPickerChange("shop",v);
                              // that.bindChange(p, v, dv);
                          },
                    });


                  });
                }, 
                setData:function(arr){
                  for( var i in arr){
                    // console.log(i);
                    // console.log(arr[i]);
                     // this.$set(i,arr[i]); 
                     this[i] = arr[i];
                  }
                },
                 //提交 
              formSubmit: function () {
                 var data =$("#BookformSubmit").serializeArray();
                  var   postData={};
                    for (var i in data) {
                        if(data[i]['value']){
                           postData[data[i]['name']]=data[i]['value'];
                        }
                    }
               
                 
                  if (this.loading == 1) {
                    return;
                  }

                  this.setData({ 'loading': 1 });
                  this.setData({ 'disabled': 1 });
                  var that = this;
                  var title = postData.title;
                  //提交预约
                  app.post("mall/dining_table_book/add", {
                    date:this.date,
                     time:this.time,
                    book_time: this.book_time,
                    dining_table_name: this.dining_table,
                    remark: postData.remark?postData.remark:'',
                    phone: postData.phone,
                    contacts: postData.contacts,
                    people: this.people,
                    shop_id: this.shop_id,
                    dining_table_id: this.dining.id,
                    money: this.dining.money,
                    use_yie: this.use_yie,
                    card_code: this.card_code,
                    method: 'POST',
                  }, function (res) {
                    that.setData({ 'loading': 0 });
                    that.setData({ 'disabled': 0 });
                    var title = "";
                    if (res.code != 200) {
                      app.alert(res.msg);
                      return ;
                    } 
                    var order_sn = res.data.order_sn;
                    // 支付
                    app.pay(order_sn);

                });
             }


            }

        }).$mount('#app');
    };
    module.exports = controller;
});
